//
//  MDDPerfectInfoViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/17.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDPerfectInfoViewController.h"
#import "DLBaiduMapViewController.h"
#import "UIViewController+XHPhoto.h"
#import "ImagePostTool.h"

@interface MDDPerfectInfoViewController ()
@property (weak, nonatomic) IBOutlet PPTextfield *nameTF;//店铺名字
@property (weak, nonatomic) IBOutlet PPTextfield *pnameTf;//联系人名字
@property (weak, nonatomic) IBOutlet PlaceholderTextView *detailAddressPT;
@property (weak, nonatomic) IBOutlet PPTextfield *addressLabel;
@property (weak, nonatomic) IBOutlet PPTextfield *phoneTf;
@property (nonatomic,strong) AxcUI_ActivityHUD * axcUI_ActivityHUD;
@property (nonatomic,strong) NSString *p;//省
@property (nonatomic,strong) NSString *s;//市
@property (nonatomic,strong) NSString *a;//区
@property (nonatomic,strong) BMKPoiInfo *pointInfo;
@property (weak, nonatomic) IBOutlet UIImageView *imageOne;
@property (weak, nonatomic) IBOutlet UIImageView *imageTwo;
@property (nonatomic,strong) NSString *postImageOne;
@property (nonatomic,strong) NSString *postImageTwo;
@property (nonatomic,assign) NSInteger selIndex;
@property (nonatomic,strong) NSString *longitude;
@property (nonatomic,strong) NSString *latitude;
@end

@implementation MDDPerfectInfoViewController


- (IBAction)submitAction:(id)sender {
    if (self.nameTF.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入店铺名字"];
        return;
    }
    if (self.pnameTf.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入联系人姓名"];
        return;
    }
    if (self.addressLabel.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请选择店铺地址"];
        return;
    }
    if (self.detailAddressPT.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入详细地址"];
        return;
    }
    if (!self.postImageOne) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请上传门头照或个人照"];
        return;
    }
    if (!self.postImageTwo) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请上传营业执照"];
        return;
    }
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    setDickeyobj(requestInfo, self.postImageTwo, @"businessLicence");
    setDickeyobj(requestInfo, self.postImageOne, @"doorImage");
    setDickeyobj(requestInfo, self.s, @"city");
    setDickeyobj(requestInfo, self.p, @"province");
    setDickeyobj(requestInfo, self.a, @"county");
    setDickeyobj(requestInfo, self.latitude, @"latitude");
    setDickeyobj(requestInfo, self.longitude, @"longitude");
    setDickeyobj(requestInfo, self.detailAddressPT.text, @"detailedAddress");
    setDickeyobj(requestInfo, self.phoneT, @"phone");
    setDickeyobj(requestInfo, self.pnameTf.text, @"contactName");
    setDickeyobj(requestInfo, self.nameTF.text, @"storeName");
    if (self.userInfo) {
        setDickeyobj(requestInfo, self.userInfo.uid, @"uid");
    }
    [PPNetworkHelper POST:app_saveUserInfo parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            [AxcUI_Toast AxcUI_showCenterWithText:@"提交成功,请耐心等待"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
}

- (IBAction)tapOneAction:(id)sender {
    WS(weakSelf);
    self.selIndex = 1;
    [self showCanEdit:YES photo:^(UIImage *photo) {
        [weakSelf updateImages:photo];
    }];
}
- (IBAction)tapTwoAction:(id)sender {
    WS(weakSelf);
    self.selIndex = 2;
    [self showCanEdit:YES photo:^(UIImage *photo) {
        [weakSelf updateImages:photo];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"资料提交");
    self.view.backgroundColor = [UIColor whiteColor];
    [self customNavigationItems];
    
    self.nameTF.maxTextLength = 20;
    self.nameTF.ppTextfieldTextChangedBlock = ^(PPTextfield *tf) {
       
    };
    self.pnameTf.maxTextLength = 20;
    self.pnameTf.ppTextfieldTextChangedBlock = ^(PPTextfield *tf) {
        
    };
    self.phoneTf.isPhoneNumber = YES;
    self.phoneTf.ppTextfieldTextChangedBlock = ^(PPTextfield *tf) {
       
    };
    self.phoneTf.userInteractionEnabled = NO;
    self.phoneTf.text = self.phoneT;
    self.detailAddressPT.placeholderLabel.font = [UIFont systemFontOfSize:14];
    self.detailAddressPT.placeholder = @"请输入详细地址";
    //               phtv.placeholderLabel.textAlignment = NSTextAlignmentCenter;
    self.detailAddressPT.placeholderFrame = CGRectMake(0, 5, mScreenWidth-140, 20);
    self.detailAddressPT.placeholderLabel.textColor = [UIColor AxcUI_colorWithHexCode:@"#B4B4B4"];
    self.detailAddressPT.font = [UIFont systemFontOfSize:14];
    self.detailAddressPT.maxLength = 100;
    self.detailAddressPT.wordNumLabel.hidden = YES;
    if (self.userInfo) {
        self.p = self.userInfo.province;
        self.s = self.userInfo.city;
        self.a = self.userInfo.county;
        self.phoneTf.text= self.userInfo.phone;
        self.nameTF.text = self.userInfo.contactName;
        self.pnameTf.text = self.userInfo.storeName;
        self.detailAddressPT.text = self.userInfo.detailedAddress;
        self.postImageOne = self.userInfo.doorImage;
        self.postImageTwo = self.userInfo.businessLicence;
        self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",self.p,self.s,self.a];
        [self.imageOne sd_setImageWithURL:[NSURL URLWithString:self.userInfo.doorImage]];
        [self.imageTwo sd_setImageWithURL:[NSURL URLWithString:self.userInfo.businessLicence]];
        self.longitude = self.userInfo.longitude;
        self.latitude = self.userInfo.latitude;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        DLBaiduMapViewController *mapVC = [[DLBaiduMapViewController alloc]init];
        mapVC.returnSelAddress = ^(BMKPoiInfo *addressInfo, NSString *p, NSString *c, NSString *a) {
            self.pointInfo = addressInfo;
            self.longitude = [NSString stringWithFormat:@"%lf",self.pointInfo.pt.longitude];
            self.latitude = [NSString stringWithFormat:@"%lf",self.pointInfo.pt.latitude];
            self.p = p;
            self.s = c;
            self.a = a;
            self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",p,c,a];
            self.detailAddressPT.text = addressInfo.address;
        };
        [self.navigationController pushViewController:mapVC animated:YES];
    }
}
-(void)updateImages:(UIImage *)header
{
    WS(weakSelf);
    [self.axcUI_ActivityHUD AxcUI_showWithText:@"正在上传头像" shimmering:YES];
    ImagePostTool * imgesPostTool = [[ImagePostTool alloc]init];
    id obj = [imgesPostTool UpLoadingForInfo:@[@{@"file":UIImageJPEGRepresentation(header, 1.0f)}]];
    if ([obj isKindOfClass:[NSDictionary class]]) {
        NSArray * info = obj[@"data"];
        if (weakSelf.selIndex == 1) {
            weakSelf.postImageOne = info.firstObject;
            self.imageOne.image = header;
        }else{
            weakSelf.postImageTwo = info.firstObject;
            self.imageTwo.image = header;
        }
    }else{
        
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        //回调或者说是通知主线程刷新，
        [self.axcUI_ActivityHUD AxcUI_dismiss];
        
    });
}

-(AxcUI_ActivityHUD *)axcUI_ActivityHUD
{
    if (!_axcUI_ActivityHUD) {
        _axcUI_ActivityHUD = [[AxcUI_ActivityHUD alloc] init];
        _axcUI_ActivityHUD.axcUI_isTheOnlyActiveView = NO;  // 能否点击背景
        _axcUI_ActivityHUD.axcUI_appearAnimationType = AxcActivityHUDAppearAnimationTypeFadeIn;       // 入场风格
        _axcUI_ActivityHUD.axcUI_disappearAnimationType = AxcActivityHUDDisappearAnimationTypeFadeOut; // 出场风格
        _axcUI_ActivityHUD.axcUI_overlayType = AxcActivityHUDOverlayTypeTransparent;    //  背景框的风格
        
    }
    return _axcUI_ActivityHUD;
}
@end
